#ifndef STX_ALGORITHM_IS_SORTED_UNTIL_HPP
#define STX_ALGORITHM_IS_SORTED_UNTIL_HPP

#include <stx/algorithm/common.hpp>

namespace stx {

template <class ForwardIterator, class Compare>
inline ForwardIterator
is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp)
{
    if (first != last) {
        ForwardIterator i = first;
        while (++i != last) {
            if (comp(*i, *first)) {
                return i;
            }
            first = i;
        }
    }
    return last;
}

template<class ForwardIterator>
inline ForwardIterator
is_sorted_until(ForwardIterator first, ForwardIterator last)
{
    return stx::is_sorted_until(first, last, stx::less<typename std::iterator_traits<ForwardIterator>::value_type>());
}

} // namespace stx

#endif // STX_ALGORITHM_IS_SORTED_UNTIL_HPP
